﻿using System;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using Team4Mock.Entity;

namespace Team4Mock.UserControl.Service
{
    public partial class AddService : System.Web.UI.UserControl
    {
        ServiceEntity se = new ServiceEntity();
        ContractEntities ce = new ContractEntities();
        FundingEntities fe = new FundingEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetCheckboxList(se.GetRef("Ser_GetAcc"), clClientAcc);
                GetCheckboxList(se.GetRef("Ser_GetBarCri"), clBarriers);
                GetCheckboxList(se.GetRef("Ser_GetBenCri"), clBenefit);
                GetCheckboxList(se.GetRef("Ser_GetClientJourney"), clClientJourrey);
                GetCheckboxList(se.GetRef("Ser_GetClientOutCome"), clClientOutCome);
                GetCheckboxList(se.GetRef("Ser_GetClientSupPro"), clSupportProcess);
                GetCheckboxList(se.GetRef("Ser_GetContractObli"), clContractObligation);
                GetCheckboxList(se.GetRef("Ser_GetContractOutCome"), clContractOutCome);
                GetCheckboxList(se.GetRef("Ser_GetDisCri"), clDisability);
                GetCheckboxList(se.GetRef("Ser_GetEthCri"), clEthnic);
                GetCheckboxList(se.GetRef("Ser_GetIntervention"), clIntervention);
                GetCheckboxList(se.GetRef("Ser_GetOthSer"), clOtherService);
                GetCheckboxList(se.GetRef("Ser_GetPerCirCri"), clPersonalCri);
                GetCheckboxList(se.GetRef("Ser_GetRefSou"), clRefSource);
                GetCheckboxList(se.GetRef("Ser_GetSerParCri"), clParticiCri);
                GetCheckboxList(se.GetRef("Ser_GetSupCen"), clSupportCenter);
                GetCheckboxList(se.GetRef("Ser_GetTarCli"), clTargetClient);
                dlProgram.DataSource = se.GetProgram();
                dlProgram.DataBind();
                if (!string.IsNullOrEmpty(Request.QueryString["CID"]))
                {
                    int id = int.Parse(Request.QueryString["CID"]);
                    se.ServiceID = id;
                    DataTable dt = new DataTable();
                    dt = se.GetdataByID(se);
                    txtSerName.Focus();
                    txtShortDes.Text = dt.Rows[0][2].ToString();
                    hdContact.Value = dt.Rows[0][4].ToString();
                    dlSubType.SelectedValue = dt.Rows[0][3].ToString();
                    if (dt.Rows[0][3].ToString().Equals("Contract"))
                    {
                        lbdiv.Text = "<li><a href=\"#Contract\" class=\"tab\">Contract</a></li>";
                    }
                    if (dt.Rows[0][3].ToString().Equals("Funding"))
                    {
                        lbdiv.Text = "<li><a href=\"#Funding\" class=\"tab\">Funding</a></li>";
                    }
                    txtClientDes.Text = dt.Rows[0][5].ToString();
                    dlAttendannce.SelectedValue = dt.Rows[0][6].ToString();
                    txtStartExp.Text = dt.Rows[0][7].ToString();
                    txtStartDate.Text = dt.Rows[0][8].ToString();
                    txtEndDate.Text = dt.Rows[0][9].ToString();
                    ckExtandable.Checked = dt.Rows[0][10].ToString().Equals("1") ? true : false;
                    txtExtenYear.Text = dt.Rows[0][11].ToString();
                    txtExtenMonth.Text = dt.Rows[0][12].ToString();
                    ckActive.Checked = dt.Rows[0][25].ToString().Equals("1") ? true : false;
                    txtDesFull.Text = dt.Rows[0][13].ToString();
                    txtDeptCode.Text = dt.Rows[0][14].ToString();
                    dlSerType.SelectedValue = dt.Rows[0][15].ToString();
                    txtDesDelivery.Text = dt.Rows[0][16].ToString();
                    txtContactCode.Text = dt.Rows[0][17].ToString();
                    txtContactValue.Text = dt.Rows[0][18].ToString();
                    ckStagedPayment.Checked = dt.Rows[0][19].ToString().Equals("1") ? true : false;
                    dlProcessMethod.SelectedValue = dt.Rows[0][20].ToString();
                    ckTimeLimited.Checked = dt.Rows[0][21].ToString().Equals("1") ? true : false;
                    txtLimitedYear.Text = dt.Rows[0][22].ToString();
                    txtLimitedMonth.Text = dt.Rows[0][23].ToString();
                    dlProgram.SelectedValue = dt.Rows[0][24].ToString();

                    
                }
            }
        }
        void GetCheckboxList(DataTable dt1, CheckBoxList cl)
        {
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                cl.Items.Add(new ListItem(dt1.Rows[i][1].ToString(), dt1.Rows[i][0].ToString()));
            }
        }
        string GetValueCheckBoxList(CheckBoxList cl)
        {
            string lb = string.Empty;
            for (int i = 0; i < cl.Items.Count; i++)
            {
                if (cl.Items[i].Selected == true)
                {
                    lb += cl.Items[i].Value + "=";
                }
            }
            return lb;
        }

        void InsertRef(string proc, CheckBoxList cl)
        {
            se.ServiceID = se.GetID();
            se.Procedure = proc;
            string a = GetValueCheckBoxList(cl);
            string[] tmp = a.Split('=');
            for (int i = 0; i < tmp.Count() - 1; i++)
            {
                se.RefID = int.Parse(tmp[i]);
                se.InsertRef(se);
            }
        }
        bool CheckNum(string a)
        {
            int number;
            bool result = Int32.TryParse(a, out number);
            if (result)
            {
                return true;
            }
            else
                return false;
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (CheckValidation() == "")
            {
                try
                {
                    se.ServiceName = txtSerName.Text;
                    if (se.CheckName(se) == true)
                    {
                        Label2.Text = "Service Name exists";
                    }
                    else
                    {
                        se.ServiceShortDes = txtShortDes.Text;
                        se.ContactID = int.Parse(hdContact.Value);
                        se.SubType = dlSubType.SelectedValue;
                        se.ClientDescription = txtClientDes.Text;
                        se.ServiceAttendance = dlAttendannce.SelectedValue;
                        se.ServiceStartExpected = txtStartExp.Text;
                        se.ServiceStartDate = txtStartDate.Text;
                        se.ServiceEndDate = txtEndDate.Text;
                        se.ServiceExtendable = ckExtandable.Checked ? 1 : 0;
                        se.ServiceExtenYears = ckExtandable.Checked ? int.Parse(txtExtenYear.Text) : 0;
                        se.ServiceExtenMonth = ckExtandable.Checked ? int.Parse(txtExtenMonth.Text) : 0;
                        se.IsActive = ckActive.Checked ? 1 : 0;
                        se.ServiceFullDes = txtDesFull.Text;
                        se.DeptCode = txtDeptCode.Text;
                        se.ServiceType = dlSerType.SelectedValue;
                        se.ServiceDesDel = txtDesDelivery.Text;
                        se.ServiceContractCode = txtContactCode.Text;
                        se.ServiceContractValue = txtContactValue.Text;
                        se.ContractStagedPayment = ckStagedPayment.Checked ? 1 : 0;
                        se.ReferralProcess = dlProcessMethod.SelectedValue;
                        se.ServiceTimeLimited = ckTimeLimited.Checked ? 1 : 0;
                        se.ServiceTimeLimitedMonth = ckTimeLimited.Checked ? int.Parse(txtLimitedMonth.Text) : 0;
                        se.ServiceTimeLimitedYear = ckTimeLimited.Checked ? int.Parse(txtLimitedYear.Text) : 0;
                        se.ProgramID = int.Parse(dlProgram.SelectedValue);
                        se.Insert(se);
                        InsertRef("Ser_InsertAcc", clClientAcc);
                        InsertRef("Ser_InsertBarCri", clBarriers);
                        InsertRef("Ser_InsertBenCri", clBenefit);
                        InsertRef("Ser_InsertClientJourney", clClientJourrey);
                        InsertRef("Ser_InsertClientSupPro", clSupportProcess);
                        InsertRef("Ser_InsertClientOutCome", clClientOutCome);
                        InsertRef("Ser_InsertDisCri", clDisability);
                        InsertRef("Ser_InsertEthCri", clEthnic);
                        InsertRef("Ser_InsertIntervention", clIntervention);
                        InsertRef("Ser_InsertOtherService", clOtherService);
                        InsertRef("Ser_InsertOthSerParCri", clParticiCri);
                        InsertRef("Ser_InsertPerCirCri", clPersonalCri);
                        InsertRef("Ser_InsertRefSou", clRefSource);
                        InsertRef("Ser_InsertSupCen", clSupportCenter);
                        InsertRef("Ser_InsertTarCli", clTargetClient);
                        Label2.Text = "Insert Service successfully";
                        if (dlSubType.SelectedValue == "Contract")
                        {
                            try
                            {
                                ce.ServiceID = se.GetID();
                                ce.Participation = dlParticipation.SelectedValue;
                                ce.Insert(ce);
                                InsertRef("Ser_InsertContractObli", clContractObligation);
                                InsertRef("Ser_InsertContractOutCome", clContractOutCome);
                                Label2.Text = "Insert Service and Contract successfully";
                            }
                            catch (Exception a)
                            {
                                Label2.Text = a.Message; //"Error while insert Contract";
                            }
                        }
                        if (dlSubType.SelectedValue == "Funding")
                        {
                            try
                            {
                                fe.FundingSource = dlFundingSource.SelectedValue;
                                fe.FunContactDetail = int.Parse(idMediaContact.Value);
                                fe.FundingAmount = txtFunAmount.Text;
                                fe.FundingStart = txtFunStart.Text;
                                fe.FundingEnd = txtFunEnd.Text;
                                fe.FunForText = txtFundaisingText.Text;
                                fe.FunWhy = txtFundaisingWhy.Text;
                                fe.FunDonorAnon = ckFundaisingDonorAnon.Checked ? 1 : 0;
                                fe.FunDonorAmount = txtFundaisingDonoAmount.Text;
                                fe.FundingNeed = txtFunNeeds.Text;
                                fe.FunConNeed = ckFunContiNeed.Checked ? 1 : 0;
                                fe.FunConAmount = txtFunContiAmount.Text;
                                fe.FunConDetail = txtFunConDetails.Text;
                                fe.FundraisingNeed = txtFundaisingNeed.Text;
                                fe.FundraisingRequiredby = txtFundaisingRequiBy.Text;
                                fe.FundraisingCompleted = ckFundaisingComplete.Checked ? 1 : 0;
                                fe.FundraisingCompletedDate = txtFundaisingCompleDate.Text;
                                fe.FundraisingDonationDate = txtFundaisingDonationDate.Text;
                                fe.FundraisingDonationIncremental = ckFundaisingDonaIncrement.Checked ? 1 : 0;
                                fe.ServiceID = se.GetID();
                                fe.Insert(fe);
                                Label2.Text = "Insert Service and Funding successfully";
                            }
                            catch (Exception a)
                            {
                                Label2.Text = a.Message; //"Error while insert Funding";
                            }
                        }
                    }

                }
                catch(Exception ax)
                {
                    Label2.Text = ax.Message;//"Error while insert Service";
                }
            }
            else
            {
                Label2.Text = CheckValidation();
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("/FormView/Service.aspx");
        }
        string CheckValidation()
        {
            string err = "";
            if (txtSerName.Text == "")
            {
                err += "Input Service Name" + "</br>";
            }
            if (txtShortDes.Text == "")
            {
                err += "Input Service Short Description" + "</br>";
            }
            if (hdContact.Value == "")
            {
                err += "Select Lead Contact" + "</br>";
            }
            if (txtDeptCode.Text == "")
            {
                err += "Input Dept Code" + "</br>";
            }
            if (dlSubType.SelectedValue == "Funding")
            {
                if (idMediaContact.Value == "")
                {
                    err += "Select Funding Contact Detail" + "</br>";
                }
                if (txtFunAmount.Text == "")
                {
                    err += "Input Funding Amount" + "</br>";
                }
            }
            if (ckExtandable.Checked==true)
            {
                if (CheckNum(txtExtenMonth.Text)==false || CheckNum(txtExtenYear.Text)==false)
                {
                    err += "Service Extendable Month and Year must is numberic"+"</br>";
                }
            }
            if (ckTimeLimited.Checked == true)
            {
                if (CheckNum(txtLimitedMonth.Text) == false || CheckNum(txtLimitedYear.Text) == false)
                {
                    err += "Service Time Limited Month and Year must is numberic" + "</br>";
                }
            }
            return err;
        }

        protected void dlSubType_SelectedIndexChanged1(object sender, EventArgs e)
        {
            if (dlSubType.SelectedIndex == 0)
            {
                lbdiv.Text = "";
            }
            if (dlSubType.SelectedIndex == 1)
            {
                lbdiv.Text = "<li><a href=\"#Contract\" class=\"tab\">Contract</a></li>";
            }
            if (dlSubType.SelectedIndex == 2)
            {
                lbdiv.Text = "<li><a href=\"#Funding\" class=\"tab\">Funding</a></li>";
            }
        }
    }
}